﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;

namespace WLMCoreLibrary
{
    //queryString = "SELECT M.*,I.archivo as nombreImagen FROM modulos M left join imagenes I on I.id=M.idImagen WHERE M.id='" + registryId + "'";
    public class MiWebSiteModules : BaseClass
    {
        #region private fields
        private String _Nombre;
        private String _URLAdmin;
        private String _URLCliente;
        private String _tablasRelacionadas;
        private int _idImagen;
        private int _orden;
        private int _idPlantilla;
        private string _nombreImagen = "";
        #endregion

        #region public properties
        public String Nombre { get { return _Nombre; } set { _Nombre = value; } }
        public String URLAdmin { get { return _URLAdmin; } set { _URLAdmin = value; } }
        public String URLCliente { get { return _URLCliente; } set { _URLCliente = value; } }
        public String tablasRelacionadas { get { return _tablasRelacionadas; } set { _tablasRelacionadas = value; } }
        public int idImagen { get { return _idImagen; } set { _idImagen = value; } }
        public int orden { get { return _orden; } set { _orden = value; } }
        public int idPlantilla { get { return _idPlantilla; } set { _idPlantilla = value; } }
        public string NombreImagem { get { return _nombreImagen; } }
        #endregion

        #region constructors
        public MiWebSiteModules(ref dbConnector ConexionDB)
        {
            
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            id = classBinder.NewRecord("modulos");
            string queryString = "";
            queryString = "SELECT * FROM modulos WHERE id=\"" + id + "\"";
            GetRecord(queryString);
        }

        public MiWebSiteModules(int registryId,ref dbConnector ConexionDB)
        {
            
            string queryString = "";
            queryString = "SELECT M.*,I.archivo as nombreImagen FROM modulos M left join imagenes I on I.id=M.idImagen WHERE M.id='" + registryId + "'";

            this.GetRecord(queryString);
        }
        #endregion

        #region public methods


        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM modulos WHERE id=\"" + id + "\"";
            ConexionDBInterna.ExecuteCommand(queryString);

        }

        public void ClearPermisions(int UserLevel)
        {
            string strCommand = "DELETE FROM modulosPermisos WHERE idNivelUsuario='" + UserLevel + "'";
            ConexionDBInterna.ExecuteCommand(strCommand);
        }
        #endregion

        #region Private Methods
        private void GetRecord(string queryString)
        {
            OdbcDataReader reader = ConexionDBInterna.ExecuteReader(queryString);
            if (reader.HasRows)
            {
                if (!reader.IsDBNull(reader.GetOrdinal("id")))
                    id = (System.Int32)reader["id"];
                if (!reader.IsDBNull(reader.GetOrdinal("idUsuario")))
                    idUsuario = (System.Int32)reader["idUsuario"];
                if (!reader.IsDBNull(reader.GetOrdinal("fechaCreacion")))
                    fechaCreacion = Convert.ToDateTime(reader["fechaCreacion"]); if (!reader.IsDBNull(reader.GetOrdinal("fechaModificacion"))) fechaModificacion = Convert.ToDateTime(reader["fechaModificacion"]); if (!reader.IsDBNull(reader.GetOrdinal("idUsuarioModificacion"))) idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];


                if (!reader.IsDBNull(reader.GetOrdinal("Nombre")))
                    _Nombre = (System.String)reader["Nombre"];
                if (!reader.IsDBNull(reader.GetOrdinal("URLAdmin")))
                    _URLAdmin = (System.String)reader["URLAdmin"];
                if (!reader.IsDBNull(reader.GetOrdinal("URLCliente")))
                    _URLCliente = (System.String)reader["URLCliente"];

                if (!reader.IsDBNull(reader.GetOrdinal("idImagen")))
                    _idImagen = (System.Int32)reader["idImagen"];
                if (!reader.IsDBNull(reader.GetOrdinal("orden")))
                    _orden = (System.Int32)reader["orden"];

                //if (!reader.IsDBNull(reader.GetOrdinal("idPlantilla")))
                //    _idPlantilla = (System.Int32)reader["idPlantilla"];
                //if (!reader.IsDBNull(reader.GetOrdinal("nombreImagen")))
                //    _nombreImagen = (System.String)reader["nombreImagen"];
            }
        }


        protected override void UpdateBind()
        {
            StringBuilder strCommand = new StringBuilder();
            strCommand.Append("UPDATE modulos set "); strCommand.Append("fechaModificacion='" + this.fechaModificacion.ToString("yyyy-MM-dd HH:mm:ss") + "'"); strCommand.Append(",idUsuarioModificacion=\"" + idUsuarioModificacion.ToString() + "\"");
            strCommand.Append("fechaModificacion='" + DateTime.UtcNow.AddHours(-6).ToString("yyyy-MM-dd HH:mm:ss") + "'");
            strCommand.Append(",Nombre=\"" + this._Nombre.ToString() + "\"");
            strCommand.Append(",URLAdmin=\"" + this._URLAdmin.ToString() + "\"");
            strCommand.Append(",URLCliente=\"" + this._URLCliente.ToString() + "\"");
            //strCommand.Append(",tablasRelacionadas=\"" + this._tablasRelacionadas.ToString() + "\"");
            //strCommand.Append(",idImagen=\"" + this._idImagen.ToString() + "\"");
            strCommand.Append(",orden=\"" + this._orden.ToString() + "\"");
            //strCommand.Append(",idPlantilla=\"" + this._idPlantilla.ToString() + "\"");
            strCommand.Append(" WHERE id=\"" + id + "\"");
            ConexionDBInterna.ExecuteCommand(strCommand.ToString());
        }
        #endregion
    }

    public class MiWebSiteModulesCollection
    {
        private dbConnector ConexionDB;
        private BaseClassArray<MiWebSiteModules> _modules;

        public BaseClassArray<MiWebSiteModules> ModulesList { get { return _modules; } }
        public MiWebSiteModulesCollection(ref dbConnector dbConn)
        {
            
            string strCommand = "SELECT * from modulos ORDER BY nombre";

            OdbcDataReader modulesReader = ConexionDB.ExecuteReader(strCommand);
            _modules = new BaseClassArray<MiWebSiteModules>();
            while (modulesReader.Read())
            {
                MiWebSiteModules newModule = new MiWebSiteModules(Convert.ToInt32(modulesReader["id"]),ref ConexionDB);
                //HttpContext.Current.Response.Write(modulesReader["nombre"].ToString());
                _modules.Add(newModule);
            }
            modulesReader.Close();
            modulesReader.Dispose();
        }

        public MiWebSiteModulesCollection(int userLevel,ref dbConnector ConexionDB)
        {
            
            string strCommand = "SELECT modulos.*,imagenes.archivo as imagen from modulos left join imagenes on modulos.idImagen=imagenes.id join modulosPermisos on modulosPermisos.idModulo=modulos.id and modulosPermisos.idNivelUsuario='" + userLevel + "' and modulosPermisos.admin=1 WHERE URLAdmin<>'/panel/default.aspx' ORDER BY orden";

            OdbcDataReader modulesReader = ConexionDB.ExecuteReader(strCommand);
            _modules = new BaseClassArray<MiWebSiteModules>();
            while (modulesReader.Read())
            {
                MiWebSiteModules newModule = new MiWebSiteModules(Convert.ToInt32(modulesReader["id"]),ref ConexionDB);
                //HttpContext.Current.Response.Write(modulesReader["nombre"].ToString());
                _modules.Add(newModule);
            }
            modulesReader.Close();
            modulesReader.Dispose();
        }

        public MiWebSiteModulesCollection(int userLevel, bool isSai,ref dbConnector ConexionDB)
        {
            
            string strCommand = "SELECT modulos.*,imagenes.archivo as imagen from modulos left join imagenes on modulos.idImagen=imagenes.id join modulosPermisos on modulosPermisos.idModulo=modulos.id and modulosPermisos.idNivelUsuario='" + userLevel + "' and modulosPermisos.cliente=1 and modulos.nombre like '%sai %' WHERE URLAdmin<>'/panel/default.aspx' ORDER BY orden";

            OdbcDataReader modulesReader = ConexionDB.ExecuteReader(strCommand);
            _modules = new BaseClassArray<MiWebSiteModules>();
            while (modulesReader.Read())
            {
                MiWebSiteModules newModule = new MiWebSiteModules(Convert.ToInt32(modulesReader["id"]),ref ConexionDB);
                //HttpContext.Current.Response.Write(modulesReader["nombre"].ToString());
                _modules.Add(newModule);
            }
            modulesReader.Close();
            modulesReader.Dispose();
        }

        public MiWebSiteModulesCollection(int userLevel, string tipo,ref dbConnector ConexionDB)
        {
            
            string strCommand = "SELECT modulos.*,imagenes.archivo as imagen from modulos left join imagenes on modulos.idImagen=imagenes.id join modulosPermisos on modulosPermisos.idModulo=modulos.id and modulosPermisos.idNivelUsuario='" + userLevel + "' and modulosPermisos." + tipo + "=1 ORDER BY orden";

            OdbcDataReader modulesReader = ConexionDB.ExecuteReader(strCommand);
            _modules = new BaseClassArray<MiWebSiteModules>();
            while (modulesReader.Read())
            {
                MiWebSiteModules newModule = new MiWebSiteModules(Convert.ToInt32(modulesReader["id"]),ref ConexionDB);
                //HttpContext.Current.Response.Write(modulesReader["nombre"].ToString());
                _modules.Add(newModule);
            }
            modulesReader.Close();
            modulesReader.Dispose();
        }

    }

    public class MiWebSiteProfile : BaseClass
    {
        private dbConnector ConexionDB;
        #region private fields
        private String _nombre;
        private int _nivelPrecio;
        #endregion

        #region public properties
        public String nombre { get { return _nombre; } set { _nombre = value; } }
        public int nivelPrecio { get { return _nivelPrecio; } set { _nivelPrecio = value; } }
        #endregion

        #region constructors
        public MiWebSiteProfile(ref dbConnector dbConn)
        {
            
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            id = classBinder.NewRecord("modulosPerfiles");
            string queryString = "";
            queryString = "SELECT * FROM modulosPerfiles WHERE id=\"" + id + "\"";
            GetRecord(queryString);
        }

        public MiWebSiteProfile(int registryId,ref dbConnector ConexionDB)
        {
            
            string queryString = "";
            queryString = "SELECT * FROM modulosPerfiles WHERE id='" + registryId + "'";
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            this.GetRecord(queryString);
        }
        #endregion

        #region public methods


        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM modulosPerfiles WHERE id=\"" + id + "\"";
            ConexionDB.ExecuteCommand(queryString);

        }
        #endregion


        #region Private Methods
        private void GetRecord(string queryString)
        {
            OdbcDataReader reader = ConexionDB.ExecuteReader(queryString);
            if (reader.HasRows)
            {
                if (!reader.IsDBNull(reader.GetOrdinal("id")))
                    id = (System.Int32)reader["id"];
                if (!reader.IsDBNull(reader.GetOrdinal("fechaCreacion")))
                    fechaCreacion = Convert.ToDateTime(reader["fechaCreacion"]); if (!reader.IsDBNull(reader.GetOrdinal("fechaModificacion"))) fechaModificacion = Convert.ToDateTime(reader["fechaModificacion"]); if (!reader.IsDBNull(reader.GetOrdinal("idUsuarioModificacion"))) idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];


                if (!reader.IsDBNull(reader.GetOrdinal("idUsuario")))
                    idUsuario = (System.Int32)reader["idUsuario"];
                if (!reader.IsDBNull(reader.GetOrdinal("nombre")))
                    _nombre = (System.String)reader["nombre"];
                if (!reader.IsDBNull(reader.GetOrdinal("nivelPrecio")))
                    _nivelPrecio = (System.Int32)reader["nivelPrecio"];
            }
        }


        protected override void UpdateBind()
        {
            StringBuilder strCommand = new StringBuilder();
            strCommand.Append("UPDATE modulosPerfiles set "); strCommand.Append("fechaModificacion='" + this.fechaModificacion.ToString("yyyy-MM-dd HH:mm:ss") + "'"); strCommand.Append(",idUsuarioModificacion=\"" + idUsuarioModificacion.ToString() + "\"");
            strCommand.Append("fechaModificacion='" + DateTime.UtcNow.AddHours(-6).ToString("yyyy-MM-dd HH:mm:ss") + "'");
            strCommand.Append(",nombre=\"" + this._nombre.ToString() + "\"");
            strCommand.Append(",nivelPrecio=\"" + this._nivelPrecio.ToString() + "\"");
            strCommand.Append(" WHERE id=\"" +id + "\"");
            ConexionDB.ExecuteCommand(strCommand.ToString());
        }
        #endregion
    }

    public class MiWebSiteProfileCollection
    {
        private dbConnector ConexionDB;
        private BaseClassArray<MiWebSiteProfile> _items;

        public BaseClassArray<MiWebSiteProfile> Elementos { get { return _items; } }
        public MiWebSiteProfileCollection(ref dbConnector conector)
        {
            string strCommand = "SELECT id FROM MODULOSPERFILES order by id";
            ConexionDB = conector;
            OdbcDataReader reader = ConexionDB.ExecuteReader(strCommand);
            _items = new BaseClassArray<MiWebSiteProfile>();
            while (reader.Read())
            {
                MiWebSiteProfile newEntry = new MiWebSiteProfile(Convert.ToInt32(reader["id"]),ref ConexionDB);

                _items.Add(newEntry);
            }
            reader.Close();
            reader.Dispose();
        }
    }

}
